<?php
    include 'config.php';
    date_default_timezone_set("Asia/Shanghai");
    if(!$_GET && !$_POST){
        echo ('id情報がありません。<br />');
        echo ('<a  href = "index.php">トップ画面へ戻る</a>');
    }else if($_GET && !$_POST){
        try{
            $dbh = dbConnect();
            $sql = "select * from diary where id=".$_GET['id'];
            $sth = $dbh->query($sql);
            $rows = $sth->fetchAll();
        }catch(PDOException $e){
            print('Error:'.$e->getMessage());
            die();
        }
    }else if($_POST){
       try{
        $dbh = dbConnect();
        $sql = "delete from diary where id=".$_POST['id'];
        $sth = $dbh->exec($sql);
        if($sth){
            header("Location:index.php");
        }
       }catch(PDOException $e){
           print('Error:'.$e->getMessage());
           die();
       }
    }
?>

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>delete</title>
    <link rel="stylesheet" href="./css/common.css">
</head>
<body class="common-body">
   <?php foreach($rows as $row):?>
    <div class="common-form-wrap">
        <form action="delete.php" method="POST">
            <input type="text" name="id" value="<?= $row['id']?>" hidden>
            <table class="common-table">
                <tr>
                    <th colspan="2"><span style="font-size: 30px;color:white;">削除を確認してください</span></th>
                </tr>
                <tr>
                    <td>TITLE</td>
                    <td><input type="text" name="title" value="<?= $row['title']?>"></td>
                </tr>
                <tr>
                    <td>CONTENT</td>
                    <td><textarea name="content" id="" cols="30" rows="10"><?= $row['content']?></textarea></td>
                </tr>
                <tr>
                    <td>ADDTIME</td>
                    <td><input type="text" readonly value="<?= '上次时间:'.$row['addtime']?>"></td>
                </tr>
                <tr class="common-footer-button">
                    <td colspan="2"><input type="submit" value="削除"></td>
                </tr>
            </table>
        </form>
    </div>
   <?php endforeach;?>
</body>
</html>